***********************************************************************************
********"Macroprudential Regulation, Quantitative Easing, and Bank Lending"********
***********************************************************************************
* This file sets up some of the datasets 
* that will be used for the 2015 analyses.
***********************************************************************************
***********************************************************************************


*==================================================================================
**# Bookmark (input)
clear *
import sas using $inputdirectory/type, case(lower)

rename abi bankid
sort bankid
labmask type, values(description)
drop description

**# Bookmark (saving dataset - bank type - e.g. joint stock company, mutual bank)
compress
save $inputdirectory/INB_type2015, replace


*==================================================================================
**# Bookmark (input)
clear *
import sas using $inputdirectory/gru, case(lower)

rename ente_segn bankid
duplicates drop gru bankid, force

* Changes suggested by the supervisory department
* LINES NOT SHOWN

**# Bookmark (saving dataset - map of the groups)
compress
save $inputdirectory/INB_gru2015, replace


*==================================================================================
**# Boomark (input)
clear *
import sas using $inputdirectory/fs

rename ABI bankid
rename VOCE voce
rename IMPORTO importo
drop DATA
keep if inlist(voce, 52000600)

format importo %20.0g
* LINE NOT SHOWN (NAMES OF BANKS/BALANCE-SHEET ITEMS ALREADY DROPPED IN THE PSEUDO DATASET)
reshape wide importo, i(bankid) j(voce)
rename importo52000600 cash
recode * (.=0)

**# Bookmark (saving dataset - cash from the financial statement)
compress
save $inputdirectory/INB_fs2015, replace


*==================================================================================
**# Bookmark (input)
clear *
import sas using $inputdirectory/netinterbank, case(lower)

reshape wide valore, i(bankid) j(vocesotvoc)
recode valore* (.=0)
replace valore5843502=-valore5843502
replace valore5843506=-valore5843506
egen netinterbank=rowtotal(valore5840536 valore5840540 valore5840548 valore5843502 valore5843506)

keep bankid netinterbank

**# Bookmark (saving dataset - net interbank position as defined in Appendix II)
compress
save $inputdirectory/INB_netintbank2015, replace


*==================================================================================
**# Bookmark (input)
clear *
import sas using $inputdirectory/capital, case(lower)

drop data_oss
rename ente_segn gru

**# Bookmark (saving dataset - consolidated capital ratios)
compress
save $inputdirectory/INB_capital2015, replace


*==================================================================================
**# Bookmark (input)
clear *
import sas using $inputdirectory/comprov

rename PROVINCIA prov
rename COMUNE comune_dip
rename REGIONE reg
rename AREGEO macro

preserve
keep if (ENDDATE>20140630)&(ENDDATE<20141231)
save xxx, replace
restore

preserve
keep if (STARTDATE>20141231&STARTDATE<=20150630)
save yyy, replace
restore

keep if (STARTDATE<=20141231&ENDDATE>20141231)
append using xxx
append using yyy

erase xxx.dta
erase yyy.dta

distinct comune_dip prov, join

duplicates tag comune_dip prov, g(dup)
sort comune_dip prov
tab dup
duplicates drop comune_dip prov, force
distinct comune_dip prov, join

keep comune_dip prov reg macro
* LINE ADDED (NOT NEEDED WITH THE TRUE DATASETS)
duplicates drop comune_dip, force

**# Bookmark (saving dataset - municipality to province mapping)
compress
save $inputdirectory/INB_comprov2015, replace
